<?php
namespace app\wxapi\controller;
use think\Controller;
use think\Request;
use think\Db;

class Wx extends Controller{
	
	protected $appid = "wx9b4f377ba93e2981";
	protected $secret = "101dca1d6874d30a5e9039b9a1631cb3";
	protected $url_AccessToken = "https://api.weixin.qq.com/cgi-bin/token";
	protected $url_SessionKey = "https://api.weixin.qq.com/sns/jscode2session";
	protected $type = "https";
	
		//获取access_token
	public function getAccessToken(Request $request){
		if($request->isPost()){
			$url = $this->url_AccessToken."?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
			$result = http_get_curl($this->type,$url,'');
			return $result;
		}else{
			return json_encode(['code'=>2,'msg'=>'网络请求错误']);
		}
	}
	
	//获取session_key
	public function getSessionKey(Request $request){
		if($request->isPost()){
			$js_code = $request->param('code');
			if($js_code){
				$url = $this->url_SessionKey."?appid=".$this->appid."&secret=".$this->secret."&js_code=".$js_code."&grant_type=authorization_code";
				$result = http_get_curl($this->type,$url,'');
				return $result;
			}
			return json_encode(['code'=>3,'msg'=>'请求参数有误']);
		}else{
			return json_encode(['code'=>2,'msg'=>'网络请求错误']);
		}
	}
	
	
}